
#import "RNFileSelector.h"
#import "FBColumnViewController.h"
#import "FBFilesTableViewController.h"
#import "FBColumnNavigationController.h"
@implementation RNFileSelector

@synthesize bridge = _bridge;

- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}
RCT_EXPORT_MODULE()


RCT_EXPORT_METHOD(Show:(nonnull NSDictionary *)props onDone:(RCTResponseSenderBlock)onDone onCancel:(RCTResponseSenderBlock)onCancel) {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
      NSString *path = [props objectForKey: @"path"];
      NSURL *url = nil;
      if ([path length] > 0) url = [NSURL URLWithString: path];
      
      
      FBFilesTableViewController *tc = [[FBFilesTableViewController alloc] initWithPath:[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject.path];
      
      FBColumnNavigationController *cnc = [[FBColumnNavigationController alloc] initWithRootViewController:tc];
      
      FBColumnViewController *columnViewController = [[FBColumnViewController alloc] initWithRootViewController:cnc];
//      UINavigationController *fileBrowser = [[UINavigationController alloc] initWithRootViewController:columnViewController];
      
      //onDone(@[[[file filePath] absoluteString]]);
      
      id<UIApplicationDelegate> app = [[UIApplication sharedApplication] delegate];
      if (columnViewController.rootNavigationController) {
            [((UINavigationController*) app.window.rootViewController) presentViewController:columnViewController.rootNavigationController animated:YES completion:nil];
        }
        else {
            [((UINavigationController*) app.window.rootViewController) presentViewController:columnViewController animated:YES completion:nil];
        }
      
      tc.selectCompletion = ^(NSString *filePath) {
        [((UINavigationController*) app.window.rootViewController) dismissViewControllerAnimated:YES completion:nil];
        onDone(@[filePath]);
      };
    });
}

@end
  
